package base.thread_demo.demo13;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 下载百度网页代码封装类
 */
public class Downloader {
    private static final Logger log = LoggerFactory.getLogger(Downloader.class);

    public static List<String> downloader() throws Exception{
        HttpURLConnection conn = (HttpURLConnection) new URL("https://www.baidu.com").openConnection();
        List<String> list = new ArrayList<>();
        try(BufferedReader reader =
                    new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))){
            String line ;
            while(null != (line=reader.readLine())){
                log.info("下载中...");
                list.add(line);
            }
        }
        return list;
    }
}
